package grum

class UserWebTests extends grails.util.WebTest {

    // Unlike unit tests, functional tests are sometimes sequence dependent.
    // Methods starting with 'test' will be run automatically in alphabetical order.
    // If you require a specific sequence, prefix the method name (following 'test') with a sequence
    // e.g. test001UserListNewDelete

    void testUserList() {
		invoke      'user/index'
		verifyTitle  'User List'
		verifyText  'User List'
	
		verifyListSize 50
    }

    void testUserNewEditDelete() {
		invoke      'user/index'
		group(description:'login as admin') {
			clickLink "login"
			setInputField(name: "username", value: "admin")
			setInputField(description: "Set password field password: adm123", name: "password", value: "adm123")
			clickButton "Login"
			
			verifyText 'login successful'
		}

		group(description:'create a new User') {
		  clickLink "ADD NEW"
	
		  verifyTitle  'Create User'
		  verifyText  'Create User'
	
		  setInputField(name: "username", value: "juhu")
		  setInputField(description: "Set password field password: juhu", name: "password", value: "juhu")
		  setInputField(name: "firstname", value: "Ju")
		  setInputField(name: "lastname", value: "Hu")
		  setInputField(name: "street", value: "Grail Str. 1")
		  setInputField(name: "zipcode", value: "12345")
		  setInputField(name: "city", value: "Holy Town")
		  clickButton "Create"
	
		  verifyXPath xpath:  "//div[@class='box']",
			      text:   /.*User.*created.*/,
			      regex:  true
		}

		group(description:'edit a User') {
		  //setInputField(description: "Set password field password: jaha", name: "password", value: "jaha")
		  setInputField(name: "firstname", value: "Jo")
		  setInputField(name: "lastname", value: "Ho")
		  setInputField(name: "street", value: "Grail Str. 2")
		  setInputField(name: "zipcode", value: "23456")
		  setInputField(name: "city", value: "Holy City")
		  clickButton "Update"
	
		  verifyXPath xpath:  "//div[@class='box']",
			      text:   /.*User.*updated.*/,
			      regex:  true
		}

		group(description:'delete a User') {
		  clickButton 'Delete'
	
		  verifyXPath xpath:  "//div[@class='box']",
			      text:   /.*User.*deleted.*/,
			      regex:  true
		}
    }

    String ROW_COUNT_XPATH = "count(//table[@class='listing']//tbody/tr)"

    void verifyListSize(int size) {
        ant.group(description:"verify User list view with $size row(s)") {
            verifyText  'User List'
            verifyXPath xpath:      ROW_COUNT_XPATH,
                        text:       size,
                        description:"$size row(s) of data expected"
        }
    }
}